package com.caow.composebase

import android.app.Application
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.os.Build.VERSION
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.annotation.ColorInt
import androidx.core.graphics.ColorUtils
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.launcher.ARouter
import com.caow.composebase.event.LogoutEvent
import com.caow.composebase.utils.UIUtils
import com.caow.composebase.widget.LoadingDialog
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import java.lang.reflect.ParameterizedType


/**
 * @Author caow
 * 2022/11/18
 */
abstract class BaseActivity<VM : BaseViewModel> :
    ComponentActivity() {

    protected val TAG by lazy { javaClass.simpleName }

    protected val viewModel: VM by lazy {
        val entityClass: Class<VM> =
            (this.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VM>

        val constructor = entityClass.getConstructor()
        constructor.newInstance()

//        val constructor = entityClass.getConstructor(Application::class.java)
//        constructor.newInstance(application)
    }

    // 加载中弹窗
    private val loadingDialog by lazy { LoadingDialog(this) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initImmersiveUtils(isImmersive())
        ARouter.getInstance().inject(this)
        lifecycle.addObserver(viewModel)
        EventBus.getDefault().register(this)
//        UIUtils.hideNavigation(window)
        initLoadingDialog()
    }

    override fun onDestroy() {
        EventBus.getDefault().unregister(this)
        super.onDestroy()
    }

    private fun initLoadingDialog() {
        viewModel.showLoadingDialog.observe(this, Observer { loadingDialog.show() })
        viewModel.dismissLoadingDialog.observe(this, Observer { loadingDialog.dismiss() })

        viewModel.closeActivity.observe(this, Observer { finish() })
    }

    protected open fun initImmersiveUtils(immersive: Boolean) {
        if (immersive) {
            if (VERSION.SDK_INT >= 21) {
                val decorView = this.window.decorView
                val option = 1280
                decorView.systemUiVisibility = option
                this.window.statusBarColor = 0
            }
            UIUtils.hideNavigation(window)
        } else {
//            this.setStatusHintDark(this.isLightColor(resources.getColor(R.color.colorPrimaryDark)))
        }
    }

    override fun isImmersive(): Boolean {
        return false
    }

    protected open fun isLoginPage(): Boolean {
        return false
    }

    private fun setStatusHintDark(bDark: Boolean) {
        if (VERSION.SDK_INT >= 23) {
            val decorView = this.window.decorView
            if (decorView != null) {
                var vis = decorView.systemUiVisibility
                vis = if (bDark) {
                    vis or 8192
                } else {
                    vis and -8193
                }
                decorView.systemUiVisibility = vis
            }
        }
    }

    private fun isLightColor(@ColorInt color: Int): Boolean {
        return ColorUtils.calculateLuminance(color) > 0.5
    }

    @Subscribe
    fun onEventLogout(event: LogoutEvent) {
        if (!isLoginPage()) {
            finish()
        }
    }
}